package cn.cvs.servlet;

import cn.cvs.dao.PowerBatteryMapper;
import cn.cvs.pojo.PowerBattery;
import cn.cvs.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/PowerUpdateServlet")
public class PowerUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        int id= (int)request.getSession().getAttribute("id");
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        PowerBattery powerBattery = new PowerBattery(
                id
               ,request.getParameter("carName")
               ,request.getParameter("cityName")
               ,request.getParameter("userName")
               ,Integer.parseInt(request.getParameter("pressureValue"))
               ,request.getParameter("time")
        );
        int i=sqlSession.getMapper(PowerBatteryMapper.class).update(powerBattery);
        if (i>0){
            response.getWriter().print("  <script>" +
                    "    alert(\"修改成功！\");" +
                    "    location.href=\"PowerListServlet\";" +
                    "  </script>");
            response.getWriter().flush();
            response.getWriter().close();
        }else {
            response.getWriter().print("  <script>" +
                    "    alert(\"修改失败！\");" +
                    "    location.href=\"PowerListServlet\";" +
                    "  </script>");
            response.getWriter().flush();
            response.getWriter().close();
        }
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
